```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java并发编程：synchronized与Lock详解</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", sans-serif;
            color: #333;
            line-height: 1.6;
        }
        h1, h2, h3, h4 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
        }
        .hero {
            background: linear-gradient(135deg, #6B73FF 0%, #000DFF 100%);
        }
        .code-block {
            background-color: #2d2d2d;
            border-radius: 8px;
            position: relative;
        }
        .code-header {
            background-color: #1e1e1e;
            border-top-left-radius: 8px;
            border-top-right-radius: 8px;
            padding: 8px 16px;
            color: #9cdcfe;
            display: flex;
            justify-content: space-between;
        }
        .mermaid {
            background-color: #f8fafc;
            padding: 20px;
            border-radius: 8px;
            margin: 20px 0;
        }
        .card {
            transition: all 0.3s ease;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 15px rgba(0, 0, 0, 0.1);
        }
        .comparison-table {
            border-collapse: separate;
            border-spacing: 0;
            width: 100%;
        }
        .comparison-table th, .comparison-table td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid #e2e8f0;
        }
        .comparison-table th {
            background-color: #f7fafc;
            font-weight: 600;
        }
        .comparison-table tr:hover td {
            background-color: #f8fafc;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero text-white py-20 px-4">
        <div class="container mx-auto max-w-5xl">
            <div class="flex flex-col items-center text-center">
                <h1 class="text-4xl md:text-5xl font-bold mb-6">Java并发编程核心机制</h1>
                <p class="text-xl md:text-2xl mb-8 max-w-3xl leading-relaxed">深入解析synchronized、Lock与Volatile的原理与应用</p>
                <div class="flex space-x-4">
                    <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full">线程安全</span>
                    <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full">同步机制</span>
                    <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full">内存模型</span>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <div class="container mx-auto max-w-5xl px-4 py-12">
        <!-- synchronized Section -->
        <section class="mb-20">
            <div class="flex items-center mb-8">
                <i class="fas fa-lock text-3xl text-blue-600 mr-4"></i>
                <h2 class="text-3xl font-bold">synchronized关键字</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div>
                    <h3 class="text-2xl font-semibold mb-4 text-gray-800">概念解析</h3>
                    <p class="text-gray-700 mb-6 leading-relaxed">synchronized是Java中的关键字，是一种同步锁。用来保证被它修饰的方法或者代码块在任意时刻只能有一个线程调用。</p>
                    
                    <h3 class="text-2xl font-semibold mb-4 text-gray-800">应用场景</h3>
                    <ul class="space-y-3 mb-6">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span>修饰代码块 - 同步语句块，作用范围是大括号内的代码，锁定调用对象</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span>修饰实例方法 - 同步方法，作用整个方法，锁定调用对象</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span>修饰静态方法 - 作用整个静态方法，锁定类的所有对象</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span>修饰类 - 作用synchronized括号内的部分，锁定类的所有对象</span>
                        </li>
                    </ul>
                </div>
                
                <div class="code-block">
                    <div class="code-header">
                        <span>Java代码示例</span>
                        <span><i class="fab fa-java mr-1"></i></span>
                    </div>
                    <pre class="p-4 text-gray-200 overflow-x-auto"><code>public class Demo01 {
    private static Object object = new Object();

    public static void main(String[] args) {
        // 锁对象
        synchronized (object) {
            // ...
        }
        // 锁类
        synchronized (Object.class) {
        }
    }

    // 同步静态方法
    public synchronized static void test1() {
        // 等同于 synchronized(Demo01.class)
    }

    // 同步实例方法
    public synchronized void test2() {
        // 等同于 synchronized(this)
    }
}</code></pre>
                </div>
            </div>
            
            <h3 class="text-2xl font-semibold mt-12 mb-6 text-gray-800">特点分析</h3>
            <div class="grid md:grid-cols-2 lg:grid-cols-3 gap-6">
                <div class="card bg-white p-6 rounded-lg">
                    <div class="flex items-center mb-3">
                        <div class="w-10 h-10 bg-blue-100 rounded-full flex items-center justify-center mr-4">
                            <i class="fas fa-atom text-blue-600"></i>
                        </div>
                        <h4 class="font-semibold">原子性</h4>
                    </div>
                    <p class="text-gray-600">操作执行不可中断，不会因线程调度而被打断</p>
                </div>
                
                <div class="card bg-white p-6 rounded-lg">
                    <div class="flex items-center mb-3">
                        <div class="w-10 h-10 bg-purple-100 rounded-full flex items-center justify-center mr-4">
                            <i class="fas fa-eye text-purple-600"></i>
                        </div>
                        <h4 class="font-semibold">可见性</h4>
                    </div>
                    <p class="text-gray-600">释放锁会写回内存，获取锁会从内存读取最新数据</p>
                </div>
                
                <div class="card bg-white p-6 rounded-lg">
                    <div class="flex items-center mb-3">
                        <div class="w-10 h-10 bg-green-100 rounded-full flex items-center justify-center mr-4">
                            <i class="fas fa-redo text-green-600"></i>
                        </div>
                        <h4 class="font-semibold">可重入锁</h4>
                    </div>
                    <p class="text-gray-600">同一线程可重复获取已经持有的锁</p>
                </div>
                
                <div class="card bg-white p-6 rounded-lg">
                    <div class="flex items-center mb-3">
                        <div class="w-10 h-10 bg-yellow-100 rounded-full flex items-center justify-center mr-4">
                            <i class="fas fa-weight-hanging text-yellow-600"></i>
                        </div>
                        <h4 class="font-semibold">重量级锁</h4>
                    </div>
                    <p class="text-gray-600">基于监视器对象实现，依赖操作系统互斥锁，性能开销大</p>
                </div>
                
                <div class="card bg-white p-6 rounded-lg">
                    <div class="flex items-center mb-3">
                        <div class="w-10 h-10 bg-red-100 rounded-full flex items-center justify-center mr-4">
                            <i class="fas fa-key text-red-600"></i>
                        </div>
                        <h4 class="font-semibold">自动加锁/释放</h4>
                    </div>
                    <p class="text-gray-600">自动获取和释放锁，简化编程模型</p>
                </div>
            </div>
            
            <h3 class="text-2xl font-semibold mt-12 mb-6 text-gray-800">内存模型图解</h3>
            <div class="mermaid">
                graph TD
                    A[线程A] -->|读取/写入| B[工作内存A]
                    C[线程B] -->|读取/写入| D[工作内存B]
                    B -->|同步| E[主内存]
                    D -->|同步| E
            </div>
        </section>
        
        <!-- Lock Section -->
        <section class="mb-20">
            <div class="flex items-center mb-8">
                <i class="fas fa-unlock-alt text-3xl text-indigo-600 mr-4"></i>
                <h2 class="text-3xl font-bold">Lock接口</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div>
                    <h3 class="text-2xl font-semibold mb-4 text-gray-800">基本使用</h3>
                    <p class="text-gray-700 mb-6 leading-relaxed">Lock是Java并发包中的接口，提供了比synchronized更灵活的锁操作。需要手动获取和释放锁，通常配合try-finally使用确保锁释放。</p>
                    
                    <div class="bg-yellow-50 border-l-4 border-yellow-400 p-4 mb-6">
                        <div class="flex">
                            <div class="flex-shrink-0">
                                <i class="fas fa-exclamation-circle text-yellow-500"></i>
                            </div>
                            <div class="ml-3">
                                <p class="text-yellow-700">必须在finally块中释放锁，否则可能导致死锁！</p>
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="code-block">
                    <div class="code-header">
                        <span>Java代码示例</span>
                        <span><i class="fab fa-java mr-1"></i></span>
                    </div>
                    <pre class="p-4 text-gray-200 overflow-x-auto"><code>// 1.获取锁对象
Lock lock = new ReentrantLock();

// 2.加锁
lock.lock(); // 获取不到锁会一直阻塞
try {
    // 3.处理业务 .....
} finally {
    // 4.释放锁
    lock.unlock();
}

// 其他加锁方式
boolean b = lock.tryLock(); // 尝试获取锁，立即返回
boolean b1 = lock.tryLock(2, TimeUnit.SECONDS); // 尝试获取锁，最多等待2秒</code></pre>
                </div>
            </div>
            
            <h3 class="text-2xl font-semibold mt-12 mb-6 text-gray-800">synchronized与Lock对比</h3>
            <div class="overflow-x-auto">
                <table class="comparison-table">
                    <thead>
                        <tr>
                            <th class="w-1/4">对比项</th>
                            <th class="w-1/4">synchronized</th>
                            <th class="w-1/4">Lock</th>
                            <th class="w-1/4">说明</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td>实现方式</td>
                            <td>Java关键字，内置语言实现</td>
                            <td>接口，需实现类如ReentrantLock</td>
                            <td>Lock更灵活但使用复杂</td>
                        </tr>
                        <tr>
                            <td>锁释放</td>
                            <td>自动释放</td>
                            <td>必须手动释放</td>
                            <td>Lock忘记释放会导致死锁</td>
                        </tr>
                        <tr>
                            <td>中断响应</td>
                            <td>不能响应中断</td>
                            <td>可以响应中断</td>
                            <td>Lock.lockInterruptibly()</td>
                        </tr>
                        <tr>
                            <td>公平性</td>
                            <td>非公平锁</td>
                            <td>可设置为公平锁</td>
                            <td>new ReentrantLock(true)</td>
                        </tr>
                        <tr>
                            <td>读写分离</td>
                            <td>不支持</td>
                            <td>支持(ReadWriteLock)</td>
                            <td>读多写少场景性能优势</td>
                        </tr>
                        <tr>
                            <td>性能</td>
                            <td>重量级锁</td>
                            <td>性能更好</td>
                            <td>JDK优化后差距缩小</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </section>
        
        <!-- Volatile Section -->
        <section class="mb-20">
            <div class="flex items-center mb-8">
                <i class="fas fa-bolt text-3xl text-orange-500 mr-4"></i>
                <h2 class="text-3xl font-bold">volatile关键字</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div>
                    <h3 class="text-2xl font-semibold mb-4 text-gray-800">概念解析</h3>
                    <p class="text-gray-700 mb-6 leading-relaxed">volatile是Java中的关键字，提供了一种稍弱的同步机制，用来确保将变量的更新操作通知到其他线程。保证变量的可见性和有序性，但不保证原子性。</p>
                    
                    <h3 class="text-2xl font-semibold mb-4 text-gray-800">Java内存模型(JMM)</h3>
                    <p class="text-gray-700 mb-6 leading-relaxed">Java内存模型定义了变量访问规则：所有变量存储在主内存中，每个线程有自己的工作内存。线程对变量的读写必须在工作内存中进行，不能直接访问主内存。</p>
                    
                    <h3 class="text-2xl font-semibold mb-4 text-gray-800">线程可见性问题</h3>
                    <p class="text-gray-700 mb-6 leading-relaxed">多个线程共享数据时，一个线程修改数据后，另一个线程可能仍使用旧值。volatile通过强制线程从主内存读取最新值来解决这个问题。</p>
                </div>
                
                <div class="code-block">
                    <div class="code-header">
                        <span>Java代码示例</span>
                        <span><i class="fab fa-java mr-1"></i></span>
                    </div>
                    <pre class="p-4 text-gray-200 overflow-x-auto"><code>public class Demo02 {
    public static void main(String[] args) {
        MyThread myThread = new MyThread();
        myThread.start();
        while (true) {
            if (myThread.getFlag()) {
                System.out.println("flag为true");
            }
        }
    }
}

class MyThread extends Thread {
    private volatile boolean flag = false; // 使用volatile保证可见性
    
    @Override
    public void run() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        flag = true;
        System.out.println("线程1修改为true");
    }
    
    public boolean getFlag() {
        return flag;
    }
}</code></pre>
                </div>
            </div>
            
            <h3 class="text-2xl font-semibold mt-12 mb-6 text-gray-800">实现原理</h3>
            <div class="mermaid">
                sequenceDiagram
                    participant 线程A
                    participant 主内存
                    participant 线程B
                    线程A->>主内存: 写入volatile变量(强制刷新)
                    主内存->>线程B: 使工作内存缓存失效
                    线程B->>主内存: 重新读取最新值
            </div>
            
            <div class="grid md:grid-cols-2 gap-8 mt-8">
                <div class="bg-blue-50 p-6 rounded-lg">
                    <h4 class="font-semibold text-lg mb-3 text-blue-800 flex items-center">
                        <i class="fas fa-lock mr-2"></i> 加锁解决方案
                    </h4>
                    <p class="text-gray-700 mb-4">使用synchronized或Lock可以保证可见性，但这是重量级解决方案，适用于需要同步的场景。</p>
                    <div class="code-block">
                        <div class="code-header">
                            <span>Java代码示例</span>
                        </div>
                        <pre class="p-4 text-gray-200 overflow-x-auto"><code>while (true) {
    synchronized (myThread) {
        if (myThread.getFlag()) {
            System.out.println("flag为true");
        }
    }
}</code></pre>
                    </div>
                </div>
                
                <div class="bg-green-50 p-6 rounded-lg">
                    <h4 class="font-semibold text-lg mb-3 text-green-800 flex items-center">
                        <i class="fas fa-bolt mr-2"></i> volatile解决方案
                    </h4>
                    <p class="text-gray-700 mb-4">volatile是轻量级解决方案，只保证可见性和有序性，但不保证原子性，适合单一变量可见性需求。</p>
                    <div class="code-block">
                        <div class="code-header">
                            <span>Java代码示例</span>
                        </div>
                        <pre class="p-4 text-gray-200 overflow-x-auto"><code>class MyThread extends Thread {
    private volatile boolean flag = false;
    // ...
}</code></pre>
                    </div>
                </div>
            </div>
        </section>
        
        <!-- Summary Section -->
        <section class="bg-gray-100 p-8 rounded-xl">
            <h3 class="text-2xl font-semibold mb-6 text-gray-800">总结对比</h3>
            <div class="grid md:grid-cols-3 gap-6">
                <div class="bg-white p-6 rounded-lg shadow-sm">
                    <div class="flex items-center mb-4">
                        <div class="w-12 h-12 bg-blue-100 rounded-full flex items-center justify-center mr-4">
                            <i class="fas fa-lock text-blue-600 text-xl"></i>
                        </div>
                        <h4 class="font-semibold text-lg">synchronized</h4>
                    </div>
                    <ul class="space-y-2 text-gray-600">
                        <li class="flex items-start">
                            <i class="fas fa-check text-blue-500 mt-1 mr-2"></i>
                            <span>Java关键字，使用简单</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check text-blue-500 mt-1 mr-2"></i>
                            <span>自动加锁/释放锁</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check text-blue-500 mt-1 mr-2"></i>
                            <span>保证原子性、可见性、有序性</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-times text-red-500 mt-1 mr-2"></i>
                            <span>非公平锁</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-times text-red-500 mt-1 mr-2"></i>
                            <span>不能响应中断</span>
                        </li>
                    </ul>
                </div>
                
                <div class="bg-white p-6 rounded-lg shadow-sm">
                    <div class="flex items-center mb-4">
                        <div class="w-12 h-12 bg-purple-100 rounded-full flex items-center justify-center mr-4">
                            <i class="fas fa-unlock-alt text-purple-600 text-xl"></i>
                        </div>
                        <h4 class="font-semibold text-lg">Lock</h4>
                    </div>
                    <ul class="space-y-2 text-gray-600">
                        <li class="flex items-start">
                            <i class="fas fa-check text-blue-500 mt-1 mr-2"></i>
                            <span>接口，实现灵活</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check text-blue-500 mt-1 mr-2"></i>
                            <span>手动加锁/释放锁</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check text-blue-500 mt-1 mr-2"></i>
                            <span>可设置为公平锁</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check text-blue-500 mt-1 mr-2"></i>
                            <span>可响应中断</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-times text-red-500 mt-1 mr-2"></i>
                            <span>使用较复杂</span>
                        </li>
                    </ul>
                </div>
                
                <div class="bg-white p-6 rounded-lg shadow-sm">
                    <div class="flex items-center mb-4">
                        <div class="w-12 h-12 bg-orange-100 rounded-full flex items-center justify-center mr-4">
                            <i class="fas fa-bolt text-orange-600 text-xl"></i>
                        </div>
                        <h4 class="font-semibold text-lg">volatile</h4>
                    </div>
                    <ul class="space-y-2 text-gray-600">
                        <li class="flex items-start">
                            <i class="fas fa-check text-blue-500 mt-1 mr-2"></i>
                            <span>轻量级同步</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check text-blue-500 mt-1 mr-2"></i>
                            <span>保证可见性、有序性</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check text-blue-500 mt-1 mr-2"></i>
                            <span>无锁机制，性能好</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-times text-red-500 mt-1 mr-2"></i>
                            <span>不保证原子性</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-times text-red-500 mt-1 mr-2"></i>
                            <span>仅适用于单一变量</span>
                        </li>
                    </ul>
                </div>
            </div>
        </section>
    </div>
    
    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-8">
        <div class="container mx-auto max-w-5xl px-4">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <h3 class="text-xl font-semibold text-white">技术小馆</h3>
                    <p class="mt-1">探索技术本质，分享编程艺术</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-blue-400 hover:text-blue-300 transition-colors duration-200">
                        <i class="fas fa-globe mr-2"></i> http://www.yuque.com/jtostring
                    </a>
                </div>
            </div>
        </div>
    </footer>
    
    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: { useMaxWidth: true },
            sequenceDiagram: { useMaxWidth: true }
        });
    </script>
</body>
</html>
```